﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RenderEngine;
using System.IO;
using RenderEngineInterfaces;
using System.Threading.Tasks;
using System.ComponentModel.Composition;
using t_TestingLibrary;
using DeepZoomPivotConstructor.VisualLib;

namespace t_RenderEngine
{
    /// <summary>
    /// Summary description for t_RenderEngineRecursiveRenderingTests
    /// </summary>
    [TestClass]
    public class t_RenderEngineRecursiveRenderingTests
    {
        public t_RenderEngineRecursiveRenderingTests()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestInitialize]
        public void TestInit()
        {
            Utilities.MyClassInit();
        }

        [TestCleanup]
        public void TestDone()
        {
            Utilities.MyClassDone();
        }

        [Export(typeof(IRenderItem))]
        [RenderInfo(typeof(Uri))]
        class SecondLevel : IRenderItem
        {
            #region IRenderItem Members

            [Import]
            private IRenderEngine RenderE { get; set; }

            public Task<Visual> Render(object what)
            {
                return Task<Visual>.Factory.StartNew(() =>
                {
                    List<Task<Visual>> items = new List<Task<Visual>>();
                    for (int i = 0; i < 4; i++)
                    {
                        items.Add(RenderE.RenderToFuture("junk.jpg"));
                    }
                    Task<Visual>.WaitAll(items.ToArray());
                    VisualHolder hld = new VisualHolder();
                    foreach (var v in items)
                    {
                        hld.AddImage(v.Result);
                    }

                    return hld;
                });
            }

            public RenderPluginPriority RenderPriority(object what)
            {
                return RenderPluginPriority.RPPGeneric;
            }

            #endregion
        }

        [TestMethod]
        public void TestSimpleDualLevel()
        {
            ///
            /// A simple guy to render one level down...
            ///
            RenderControl re = new RenderControl();
            t_RenderTestingLibrary.Utilities.AddJPEGRender();
            Utilities.AddPart(new SecondLevel());
            Utilities.Compose(re);

            Visual result = re.Render(new Uri("http://www.fnal.gov"));
            Assert.IsNotNull(result, "Should have done a rendering here! What happened?");
            Assert.AreEqual(4, result.SGNodeList.Count, "Got improper number of jpegs rendered!");
        }
    }
}
